import Compressor from "compressorjs";
export const useCompressor = async (file: File | File[]) => {
  if (Array.isArray(file)) {
    const List = [];
    for (let p of file) {
      const data = await getCompressor(p);
      List.push(data);
    }
    return List;
  } else {
    return getCompressor(file);
  }
};
const getCompressor = (file: File, quality = 0.5) => {
  return new Promise((resolve: (value: File) => void) => {
    const { name, type } = file;
    new Compressor(file, {
      quality,
      success: (result) => {
        if (result instanceof File) {
          resolve(result);
        } else {
          resolve(
            new File([result], name, {
              type,
            })
          );
        }
      },
    });
  });
};
